﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class BaoCaoTonThangDAO
    {
        public int Insert(
            System.Data.SqlClient.SqlConnection connection,
            System.Data.SqlClient.SqlTransaction transaction,
            DTO.BaoCaoTonThangDTO dto)
        {
            int r = -1;
            try
            {
                List<System.Data.SqlClient.SqlParameter> prs =
                   new List<System.Data.SqlClient.SqlParameter>();
                prs.Add(new System.Data.SqlClient.SqlParameter("@Thang", dto.Thang));
                prs.Add(new System.Data.SqlClient.SqlParameter("@IDSach", dto.IDSach));
                prs.Add(new System.Data.SqlClient.SqlParameter("@TonDau", dto.TonDau));
                prs.Add(new System.Data.SqlClient.SqlParameter("@TonCuoi", dto.TonCuoi));
                prs.Add(new System.Data.SqlClient.SqlParameter("@PhatSinh", dto.PhatSinh));
                if (connection == null || transaction == null)
                    r = DAL.sqlHelper.ExecuteNoneQuery("BaoCaoTonThang_Insert", prs);
                else
                    r = DAL.sqlHelper.ExecuteNoneQuery(connection, transaction, "BaoCaoTonThang_Insert", prs);
                return r;
            }
            catch (System.Exception ex)
            {
                throw new System.Exception("Insert failed " + ex.Message);
            }
            finally
            {
            }
        }

        public List<DTO.BaoCaoTonThangDTO> SelectByThang(
            System.Data.SqlClient.SqlConnection connection,
            System.Data.SqlClient.SqlTransaction transaction,
            int Thang)
        {
            List<DTO.BaoCaoTonThangDTO> r = new List<DTO.BaoCaoTonThangDTO>();
            try
            {
                List<System.Data.SqlClient.SqlParameter> prs =
                    new List<System.Data.SqlClient.SqlParameter>();
                prs.Add(new System.Data.SqlClient.SqlParameter("@Thang", Thang));
                System.Data.DataTable dt = null;
                if (connection == null || transaction == null)
                    dt = DAL.sqlHelper.ExecuteQuery("BaoCaoTon_SelectAllByThang", prs);
                else
                    dt = DAL.sqlHelper.ExecuteQuery(connection, transaction,
                        "BaoCaoTon_SelectAllByThang", prs);
                foreach (System.Data.DataRow dr in dt.Rows)
                {
                    DTO.BaoCaoTonThangDTO dto = new DTO.BaoCaoTonThangDTO();
                    dto.Thang = int.Parse(dr["Thang"].ToString());
                    dto.IDSach = int.Parse(dr["IDSach"].ToString());
                    dto.PhatSinh = int.Parse(dr["PhatSinh"].ToString());
                    dto.TonDau = int.Parse(dr["TonDau"].ToString());
                    dto.TonCuoi = int.Parse(dr["TonCuoi"].ToString());
                    r.Add(dto);
                }
                return r;
            }
            catch (System.Exception ex)
            {
                throw new System.Exception("SelectByThang failed " + ex.Message);
            }
            finally
            {
            }
        }
    }
}
